
package com.zoio.saike.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import com.zoio.saike.model.*;

import com.zoio.saike.service.*;
import com.zoio.saike.vo.query.*;

/**
 * @author Bryan.Zhang@ZOIO.NET
 * @date 1/2/2011
 * 注解说明：@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作
 * 注解说明：@Valid标注Spring在绑定对象时自动为我们验证对象属性并存放errors在BindingResult
 */

@Controller
@RequestMapping("/amfile")
public class AmFileController extends BaseRestSpringController<AmFile,java.lang.String>{
	protected static final String DEFAULT_SORT_COLUMNS = null; 	//example: username desc,createTime asc
	private final String LIST_ACTION = "redirect:/amfile";
	
	private AmFileManager amFileManager;
	public void setAmFileManager(AmFileManager manager) {
		this.amFileManager = manager;
	}
	
	/** binder用于bean属性的设置 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 列表 */
	@RequestMapping
	public String index(ModelMap model,AmFileQuery query,HttpServletRequest request,HttpServletResponse response) {
		Page<AmFile> page = this.amFileManager.findPage(query);
		
		model.addAllAttributes(toModelMap(page, query));
		return "/amfile/index";
	}
	
	/** 显示 */
	@RequestMapping(value="/{id}")
	public String show(ModelMap model,@PathVariable java.lang.String id) throws Exception {
		AmFile amFile = (AmFile)amFileManager.getById(id);
		model.addAttribute("amFile1",amFile);
		return "/amfile/show";
	}

	/** 进入新增 */
	@RequestMapping(value="/new")
	public String _new(ModelMap model,AmFile amFile,HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("amFile",amFile);
		return "/amfile/new";
	}
	
	/** 保存新增  */
	@RequestMapping(method=RequestMethod.POST)
	public @ResponseBody String create(ModelMap model,@Valid AmFile amFile,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) return  "";
		
		try{
			amFileManager.save(amFile);
			return "success";
		}catch (Exception ex) {
			return ex.getMessage();
	    }   
	}
	
	/** 编辑 */
	@RequestMapping(value="/{id}/edit")
	public String edit(ModelMap model,@PathVariable java.lang.String id) throws Exception {
		AmFile amFile = (AmFile)amFileManager.getById(id);
		model.addAttribute("amFile",amFile);
		return "/amfile/edit";
	}
	
	/** 保存更新 */
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public @ResponseBody String update(ModelMap model,@PathVariable java.lang.String id,@Valid AmFile amFile,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) return  "";
		
		try{
			amFileManager.update(amFile);
			return "success";
		}catch (Exception ex) {
			return ex.getMessage();
	    }  
	}
	
	/** 删除 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public String delete(ModelMap model,@PathVariable java.lang.String id) {
		doDelete(amFileManager,new java.lang.Integer[]{new Integer(id)});
		
		return LIST_ACTION;
	}

	/** 批量删除 */
	@RequestMapping(method=RequestMethod.DELETE)
	public String batchDelete(ModelMap model,@RequestParam("items") java.lang.String[] items) {
		doDelete(amFileManager,items);
		
		return LIST_ACTION;
	}
	
}

